package com.ben.chatroom.common.util;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import netscape.javascript.JSObject;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

/**
 * @author huangyijun
 */
public class ObjectUtil {

  public static byte[] Object2JsonBytes(Object obj) {
    String json = ObjectToJson(obj);
    return json.getBytes(StandardCharsets.UTF_8);
  }

  public static <T> T JsonBytes2Object(byte[] bytes, Class<T> clazz) {
    String json = new String(bytes, StandardCharsets.UTF_8);
    return JsonToObject(json, clazz);
  }

  public static String ObjectToJson(Object obj) {
    return new Gson().toJson(obj);
  }

  public static <T> T JsonToObject(String json, Class<T> clazz) {
    return JSON.parseObject(json, clazz);
  }

}
